{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 4.2 \u4ee3\u7406\u8fed\u4ee3\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6784\u5efa\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5bb9\u5668\u5bf9\u8c61\uff0c\u91cc\u9762\u5305\u542b\u6709\u5217\u8868\u3001\u5143\u7ec4\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\n\u4f60\u60f3\u76f4\u63a5\u5728\u4f60\u7684\u8fd9\u4e2a\u65b0\u5bb9\u5668\u5bf9\u8c61\u4e0a\u6267\u884c\u8fed\u4ee3\u64cd\u4f5c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b9e\u9645\u4e0a\u4f60\u53ea\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a __iter__() \u65b9\u6cd5\uff0c\u5c06\u8fed\u4ee3\u64cd\u4f5c\u4ee3\u7406\u5230\u5bb9\u5668\u5185\u90e8\u7684\u5bf9\u8c61\u4e0a\u53bb\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Node:\n    def __init__(self, value):\n        self._value = value\n        self._children = []\n\n    def __repr__(self):\n        return 'Node({!r})'.format(self._value)\n\n    def add_child(self, node):\n        self._children.append(node)\n\n    def __iter__(self):\n        return iter(self._children)\n\n# Example\nif __name__ == '__main__':\n    root = Node(0)\n    child1 = Node(1)\n    child2 = Node(2)\n    root.add_child(child1)\n    root.add_child(child2)\n    # Outputs Node(1), Node(2)\n    for ch in root:\n        print(ch)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c __iter__() \u65b9\u6cd5\u53ea\u662f\u7b80\u5355\u7684\u5c06\u8fed\u4ee3\u8bf7\u6c42\u4f20\u9012\u7ed9\u5185\u90e8\u7684 _children \u5c5e\u6027\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Python\u7684\u8fed\u4ee3\u5668\u534f\u8bae\u9700\u8981 __iter__() \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u5b9e\u73b0\u4e86 __next__() \u65b9\u6cd5\u7684\u8fed\u4ee3\u5668\u5bf9\u8c61\u3002\n\u5982\u679c\u4f60\u53ea\u662f\u8fed\u4ee3\u904d\u5386\u5176\u4ed6\u5bb9\u5668\u7684\u5185\u5bb9\uff0c\u4f60\u65e0\u987b\u62c5\u5fc3\u5e95\u5c42\u662f\u600e\u6837\u5b9e\u73b0\u7684\u3002\u4f60\u6240\u8981\u505a\u7684\u53ea\u662f\u4f20\u9012\u8fed\u4ee3\u8bf7\u6c42\u65e2\u53ef\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\u7684 iter() \u51fd\u6570\u7684\u4f7f\u7528\u7b80\u5316\u4e86\u4ee3\u7801\uff0c\niter(s) \u53ea\u662f\u7b80\u5355\u7684\u901a\u8fc7\u8c03\u7528 s.__iter__() \u65b9\u6cd5\u6765\u8fd4\u56de\u5bf9\u5e94\u7684\u8fed\u4ee3\u5668\u5bf9\u8c61\uff0c\n\u5c31\u8ddf len(s) \u4f1a\u8c03\u7528 s.__len__() \u539f\u7406\u662f\u4e00\u6837\u7684\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}